package com.cht.sys;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import com.cht.sys.to.LDAPResult;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

public class LDAP {
	
	static private String SPLIT = "UUUU";	//回傳的是ArrayList時，需要分割的符號
	static private String url = "http://10.80.71.72:8056/UtilWeb/";
	
	public LDAP() {
		//取得連線的位址
		/*Properties prop = new Properties();
		InputStream ins = null;
		try {
			ins  = LDAP.class.getResourceAsStream("/authentication.properties");
			prop.load(ins);
			url = prop.getProperty("url");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (ins != null) { try { ins.close(); } catch (IOException e) { e.printStackTrace(); } }
		}*/
	}
	public static LDAPResult getEmployeeInfoByEmpNO(String employeeNumber) throws Exception {
		String params = "?employeeNumber="+employeeNumber;
		return new Gson().fromJson( queryServlet("GetEmployeeInfoByEmpNO",params), LDAPResult.class);
	}
	
	public static LDAPResult checkEmpNOAndPwd(String employeeNumber,String passwd) throws JsonSyntaxException, Exception {
		String params = "?employeeNumber="+employeeNumber+"&passwd="+passwd;
		return new Gson().fromJson( queryServlet("CheckEmpNOAndPwd",params), LDAPResult.class);
	}
	
	public static String getEmail(String employeeNumber) throws Exception {
		String params = "?employeeNumber="+employeeNumber;
		return queryServlet("GetEmailByAuthentication",params);
	}
	
	public static String getEmployeeNumber(String uid) throws Exception {
		String params = "?uid="+uid;
		return queryServlet("GetEmployeeNumber",params);
	}
	
	public static ArrayList getEmployeeNumberByName(String name) throws Exception {
		String params = "?split="+SPLIT+"&name="+name;
		String tmp[] = queryServlet("GetEmployeeNumberByName",params).split( SPLIT );
		ArrayList arr = new ArrayList();
		for(int i=0; i<tmp.length;i++) {
			arr.add(tmp[i]);
		}
		return arr;
	}
	
	public static ArrayList getDirectoryInfo(String directoryDN,String directoryValue) throws Exception {
		String params = "?directoryDN="+directoryDN+"&split="+SPLIT+"&directoryValue="+directoryValue;
		String tmp[] = queryServlet("GetDirectoryInfo",params).split( SPLIT );
		ArrayList arr = new ArrayList();
		for(int i=0; i<tmp.length;i++) {
			arr.add(tmp[i]);
		}
		return arr;
	}
	
	public static String getDirectoryNameByValue(String value) throws Exception {
		String params = "?value="+value;
		return queryServlet("GetDirectoryNameByValue",params);
	}
	
	public static String getMobileNum(String employeeNumber) throws Exception {
		String params = "?employeeNumber="+employeeNumber;
		return queryServlet("GetMobileNum",params);
	}
	public static String getName(String employeeNumber) throws Exception {
		String params = "?employeeNumber="+employeeNumber;
		return queryServlet("GetName",params);
	}
	public static String getOU(String employeeNumber) throws Exception {
		String params = "?employeeNumber="+employeeNumber;
		return queryServlet("GetOU",params);
	}
	public static String getOUCN(String employeeNumber) throws Exception {
		String params = "?employeeNumber="+employeeNumber;
		return queryServlet("GetOUCN",params);
	}
	public static String getTelephoneNumber(String employeeNumber) throws Exception {
		String params = "?employeeNumber="+employeeNumber;
		return queryServlet("GetTelephoneNumber",params);
	}
	public static String getUtf8cn(String employeeNumber) throws Exception {
		String params = "?employeeNumber="+employeeNumber;
		return queryServlet("GetUtf8cn",params);
	}
	
	private static String queryServlet(String method,String params) throws Exception {
		InputStream ins = null;
		String res = "";
		try {
			URL gwtServlet = new URL(url+method+params);
			HttpURLConnection servletConnection = (HttpURLConnection) gwtServlet.openConnection();
			ins = servletConnection.getInputStream();
			res = convertStreamToString(ins);
			if ( res.equalsIgnoreCase("") ) {
				res = null;	
			}	
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			if (ins != null) { try { ins.close(); } catch (IOException e) { e.printStackTrace(); } }
		}
		return res;
	}
	
	
	//轉換inputStream 為 字串
	private static String convertStreamToString(InputStream is) throws IOException {
		if (is != null) {
			Writer writer = new StringWriter();

			char[] buffer = new char[1024];
			try {
				Reader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
				int n;
				while ((n = reader.read(buffer)) != -1) {
					writer.write(buffer, 0, n);
				}
			} finally { is.close(); }
			return writer.toString();
		} else { return ""; }
	}
}
